<template>
  <div class="table-main me_sele_width">
    <el-dialog
      :close-on-click-modal="false"
      @close="handMyClose"
      :append-to-body="true"
      title="商品规格"
      :visible.sync="dialogVisible3"
      width="60%"
    >
      <div class="my_padd" v-loading="loading2">
        <el-row type="flex" class="row-bg" align="middle">
          <el-col :span="3">商品规格</el-col>
          <el-col :span="6">
            <el-button type="primary" @click="handSpec()">添加规格</el-button>
          </el-col>
        </el-row>
        <div v-if="byGoodsIdList.length == 0" style="text-align: center;">暂无数据</div>
        <div v-if="byGoodsIdList.length != 0">
          <el-row
            v-for="item in byGoodsIdList"
            :key="item.id"
            :gutter="20"
            type="flex"
            class="row-bg"
            align="middle"
          >
            <el-col :span="3"></el-col>
            <el-col :span="18">
              <div class="dis_span">
                <div class="dis_span2">
                  <el-tag
                    :key="item.id"
                    closable
                    color="#38ad93"
                    size="medium"
                    :disable-transitions="false"
                    @close="handleTagClose(item.id)"
                  >{{ item.typeName }}</el-tag>
                </div>

                <div style="margin-left:10px;" class="chid_bor">
                  <el-tag
                    size="small"
                    type="success"
                    :key="item2.id"
                    v-for="item2 in item.goodAttrList"
                    closable
                    :disable-transitions="false"
                    @close="handleTagClose(item2.id)"
                  >{{ item2.typeName }}</el-tag>

                  <el-input
                    class="input-new-tag"
                    v-if="inputVisible == item.id"
                    v-model="inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="$event.target.blur"
                    @blur="handleInputConfirm(item.id)"
                  ></el-input>
                  <!-- @blur="handleInputConfirm(item.id)" -->
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(item.id)"
                  >+ 添加参数</el-button>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>

        <div v-if="tableData.length != 0">
          <el-table
            @row-click="handleCurrentChange"
            :data="tableData"
            border
            style="width: 100%"
            height="500"
          >
            <el-table-column prop="attrName" label="组合名字" width="180"></el-table-column>
            <el-table-column prop="createTime" label="日期" width="180"></el-table-column>
            <el-table-column prop="price" label="价格" width="180">
              <template scope="scope">
                <el-input
                  v-if="isShow == scope.row.id"
                  size="small"
                  v-model="scope.row.price"
                  placeholder="请输入内容"
                  @change="handleEdit(scope.$index, scope.row)"
                  @blur="handEditGuiGe(scope.row)"
                ></el-input>
                <span v-else>{{ scope.row.price }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="nowPrice" label="现价" width="180">
              <template scope="scope">
                <el-input
                  v-if="isShow == scope.row.id"
                  size="small"
                  v-model="scope.row.nowPrice"
                  placeholder="请输入内容"
                  @change="handleEdit(scope.$index, scope.row)"
                  @blur="handEditGuiGe(scope.row)"
                ></el-input>
                <span v-else>{{ scope.row.nowPrice }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="company" label="单位" width="180">
              <template scope="scope">
                <el-select
                  v-if="isShow == scope.row.id"
                  @change="handSelChange($event, scope.row)"
                  v-model="scope.row.company"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in companyList"
                    :key="item.id"
                    :label="item.dataValue"
                    :value="item.id"
                  ></el-option>
                </el-select>
                <span v-else>{{ scope.row.companyStr }}</span>
              </template>
            </el-table-column>
            <!-- <el-table-column prop="companyStr" label="单位字符串"></el-table-column> -->
            <el-table-column prop="stock" label="库存" width="180">
              <template scope="scope">
                <el-input
                  v-if="isShow == scope.row.id"
                  size="small"
                  v-model="scope.row.stock"
                  placeholder="请输入内容"
                  @change="handleEdit(scope.$index, scope.row)"
                  @blur="handEditGuiGe(scope.row)"
                ></el-input>
                <span v-else>{{ scope.row.stock }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="goodsWeight" label="商品重量" width="180">
              <template scope="scope">
                <el-input
                  v-if="isShow == scope.row.id"
                  size="small"
                  v-model="scope.row.goodsWeight"
                  placeholder="请输入内容"
                  @change="handleEdit(scope.$index, scope.row)"
                  @blur="handEditGuiGe(scope.row)"
                ></el-input>
                <span v-else>{{ scope.row.goodsWeight }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="goodsVolume" label="商品体积" width="180">
              <template scope="scope">
                <el-input
                  v-if="isShow == scope.row.id"
                  size="small"
                  v-model="scope.row.goodsVolume"
                  placeholder="请输入内容"
                  @change="handleEdit(scope.$index, scope.row)"
                  @blur="handEditGuiGe(scope.row)"
                ></el-input>
                <span v-else>{{ scope.row.goodsVolume }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="sellNum" label="售出数量" width="180">
              <template scope="scope">
                <el-input
                  v-if="isShow == scope.row.id"
                  size="small"
                  v-model="scope.row.sellNum"
                  placeholder="请输入内容"
                  @change="handleEdit(scope.$index, scope.row)"
                  @blur="handEditGuiGe(scope.row)"
                ></el-input>
                <span v-else>{{ scope.row.sellNum }}</span>
              </template>
            </el-table-column>
            <!-- <el-table-column prop="isFreeShipping" label="是否包邮" width="180">
              <template scope="scope">
                <el-select
                  v-if="isShow == scope.row.id"
                  v-model="scope.row.isFreeShipping"
                  placeholder="请选择"
                >
                  <el-option label="是" :value="1"></el-option>
                  <el-option label="否" :value="2"></el-option>
                </el-select>
                <span v-else>
                  {{
                  scope.row.isFreeShipping == 1 ? "是" : "否"
                  }}
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="postage" label="邮费" width="180">
              <template scope="scope">
                <el-input
                  v-if="isShow == scope.row.id"
                  size="small"
                  v-model="scope.row.postage"
                  placeholder="请输入内容"
                  @change="handleEdit(scope.$index, scope.row)"
                  @blur="handEditGuiGe(scope.row)"
                ></el-input>
                <span v-else>{{ scope.row.postage }}</span>
              </template>
            </el-table-column>-->
            <el-table-column prop="mainImg" label="规格主图" width="180">
              <template scope="scope">
                <el-upload
                  :headers="headers"
                  accept=".jpg, .png, .gif.svg, .bmp, .jpeg"
                  v-if="isShow == scope.row.id"
                  class="avatar-uploader"
                  :action="$store.state.app.newUrl + '/admin/wxUser/uoloadfile'"
                  :show-file-list="false"
                  :on-success="
                    (file, fileList) => {
                      handSuccess(file, fileList, scope.row);
                    }
                  "
                  :before-upload="beforeAvatarUpload"
                >
                  <img
                    v-if="scope.row.mainImg != undefined"
                    :src="scope.row.mainImg"
                    class="avatar"
                  />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                <img v-else :src="scope.row.mainImg" class="avatar" />
              </template>
            </el-table-column>
            <!-- <el-table-column prop="fingerprint" label="图片指纹" width="180"></el-table-column> -->
            <el-table-column label="操作" width="80" fixed="right">
              <template scope="scope">
                <el-button
                  v-if="scope.row.id == isShow"
                  type="success"
                  icon="el-icon-check"
                  circle
                  @click="handEditShow(scope.row)"
                ></el-button>
                <el-button
                  type="primary"
                  v-else
                  icon="el-icon-edit"
                  circle
                  @click="handEditShow(scope.row)"
                ></el-button>

                <!-- <el-button
                  type="text"
                  @click="handEditShow(scope.row)"
                >{{scope.row.id==isShow?'提交':'编辑'}}</el-button>-->
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>

      <span v-if="isShow != 3" slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible3 = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { imgSizi, ifObj, ifObjTow } from '@/utils/myUtils'
export default {
  name: 'SidebarLogo',
  components: {},
  watch: {},

  props: {
    specId: {
      type: [Number, String],
      default: 0,
    },
  },
  data() {
    return {
      headers: {
        token: localStorage.getItem('mytoken') || '',
      },
      loading2: true,
      isShow: '',
      tableData: [],
      dynamicTags: ['标签一', '标签二', '标签三'],
      inputVisible: '',
      inputValue: '',
      form: {},
      isShow: 0,
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible3: false,
      addressKeyword: '',
      activeName: 'first',
      byGoodsIdList: [],
      companyList: [],
    }
  },
  mounted() {
    // this.$api.getGoodsListl().then(res => {
    //   this.shopStatusOptions = res.data.data
    //   console.log(res, '获取所有的商品')
    // })
  },
  methods: {
    // getFindCompany
    handSelChange(val, row) {
      if (this.companyList) {
        this.companyList.forEach((item) => {
          if (item.id == val) {
            console.log(val, row, item.id, item)
            row.companyStr = item.dataValue
          }
        })
      }
    },
    handEditShow(row) {
      console.log(row)

      if (this.isShow == row.id) {
        let flag = ifObj(row)

        try {
          ifObjTow(row)
          var date = row.createTime
          date = date.substring(0, 19)
          date = date.replace(/-/g, '/')
          var timestamp = new Date(date).getTime()
          row.createTime = timestamp
          this.$api.getEditGoodAttrDsetaileds(row).then((res) => {
            this.handGetFindGoodAttrDetaileAll(this.specId)
            this.isShow = ''
          })
        } catch (error) {
          this.$message({
            type: 'warning',
            message: '参数不完整',
          })
        }
      } else {
        this.handGetFindGoodAttrDetaileAll(this.specId)
        this.isShow = row.id
      }
    },
    handEditGuiGe(item) {},
    handleCurrentChange(row, event, column) {
      // console.log(row)
      // this.isShow = row.id
    },
    handleEdit(index, row) {
      console.log(index, row)
    },
    handleDelete(index, row) {
      console.log(index, row)
    },
    handGetFindGoodAttrDetaileAll(id) {
      this.loading2 = true
      this.isShow = ''
      this.$api
        .getFindGoodAttrDetaileAll(id)
        .then((res) => {
          if (res.data.code == 200) {
            this.loading2 = false
            console.log(res, '获取组合信息', this.loading2)
            this.tableData = res.data.data
            // this.byGoodsIdList = res.data.data
          }
        })
        .finally(() => {
          this.loading2 = false
        })
    },
    //设置参数
    handleTagClose(id) {
      this.loading2 = true
      this.$api
        .getDelGoodAttr(id)
        .then((res) => {
          this.handGetByGoodId(this.specId)
          if (res.data.code == 200) {
            console.log(res, '删除规格')
            if (res.data.code == 200) {
              // this.$message(res.data.message)
              this.$message({
                message: res.data.message,
                type: 'success',
              })
              // this.$refs[`popover-` + row.id].doClose()
              // this.init()
            }
            // this.byGoodsIdList = res.data.data
          }
        })
        .finally(() => {
          this.loading2 = false
        })
    },

    showInput(id) {
      this.inputVisible = id
      this.$nextTick((_) => {
        // this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm(id) {
      if (!this.inputValue) {
        this.$message({
          message: '规格参数不能为空',
        })
        this.inputVisible = ''
        return
      }
      this.loading2 = true
      var obj = {
        goodId: this.specId,
        parentId: id,
        typeName: this.inputValue,
      }
      this.$api
        .getAddGoodAttr(obj)
        .then((res) => {
          this.handGetByGoodId(this.specId)
          console.log(res, '添加参数规格')
          // this.byGoodsIdList = res.data.data
          this.loading2 = false
        })
        .finally(() => {
          this.loading2 = false
        })

      // let inputValue = this.inputValue
      // if (inputValue) {
      //   this.dynamicTags.push(inputValue)
      // }
      this.inputVisible = ''
      this.inputValue = ''
    },
    //获取规格
    async handGetByGoodId(id) {
      try {
        this.loading2 = true
        this.byGoodsIdList = (await this.$api.getByGoodId(id)).data.data
        this.companyList = (await this.$api.getFindCompany(id)).data.data
        console.log(this.companyList, '规格')
        this.tableData = (
          await this.$api.getFindGoodAttrDetaileAll(id)
        ).data.data
        this.loading2 = false
      } catch (error) {
        console.log(error)
        this.loading2 = false
      }
    },
    handSpec() {
      this.$prompt('请输入规格', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          console.log(action)
          if (action == 'confirm') {
            console.log(instance.inputValue, 'sss', action)
            var obj = {
              goodId: this.specId,
              parentId: '0',
              typeName: instance.inputValue,
            }
            this.$api.getAddGoodAttr(obj).then((res) => {
              this.handGetByGoodId(this.specId)
              console.log(res, '添加第一规格')
              done()
            })
          } else {
            done()
          }
        },
        // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        // inputErrorMessage: '邮箱格式不正确'
      })
        .then(({ value }) => {
          // getAddGoodAttr
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入',
          })
        })
    },
    //关闭弹框回调
    handMyClose() {},
    handFromSub() {},
    handleClose() {
      this.dialogVisible3 = false
    },
    handleClick() {},
    handSuccess(res, file, row) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      // this.form.mainImg = res.message
      console.log(res, file, '????', imgUrl)
      let imgUrl = JSON.parse(res.message)

      this.$set(row, 'mainImg', imgUrl.url)
      this.$set(row, 'fingerprint', 'wwww')
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      const isJPG = file.type === 'image/jpeg'
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      const isSize = imgSizi(file, 378, 378)
      return isSize
    },
  },
}
</script>

<style lang="scss" scoped>
.my_padd {
  /deep/ .el-select.el-select--medium {
    width: auto;
  }
}
.dis_span2 {
  /deep/ .el-tag--medium {
    height: 38px;
    line-height: 36px;
  }
}

.dis_span2 {
  display: flex;
  align-items: center;
}
.chid_bor {
  border: 1px solid #eee;
  border-radius: 5px;
  padding: 8px;
}
.dis_span {
  display: flex;
  align-content: center;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.row-bg {
  margin-bottom: 30px;
}
.my_padd {
  padding-top: 30px;
  padding-left: 30px;
}
.map_btn {
  width: 82px;
  height: 22px;
  background: rgba(56, 173, 147, 1);
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  line-height: 22px;
  text-align: center;
  margin-top: 8px;
}
/deep/ .el-form-item__label {
  font-size: 14px;
  color: #000;
  font-weight: bold;
}
.form_box {
  width: 80%;
  margin: 0 auto;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #38ad93;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #38ad93;
  width: 60px;
  height: 60px;
  line-height: 60px;
  text-align: center;
}
.avatar {
  width: 60px;
  height: 60px;
  display: block;
}
</style>
